package ${packageName}.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${packageName}.entity.${entityName};
<#if dtoFlag=='Y'>
import ${packageName}.dto.${entityName}DTO;
</#if>
import ${packageName}.mapper.${entityName}Mapper;
import ${packageName}.service.I${entityName}Service;
import ${packageCoreName}.common.lang.StringToolUtils;
<#if dtoFlag=='Y'>
import org.springframework.beans.BeanUtils;
</#if>
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
<#if dtoFlag=='Y'>
import java.util.HashMap;
import java.util.Map;
</#if>
/**
* @author Faye.Wang
* @version 1.0
* @date 2021/1/6 20:09
* @Description
*/
@Service
public class I${entityName}ServiceImpl implements I${entityName}Service {

	@Autowired
	private ${entityName}Mapper ${entityNameVar}Mapper;

	@Transactional(readOnly = false,rollbackFor = Exception.class,propagation= Propagation.REQUIRED)
	@Override
	public void insertEntity(${entityName} record) {
		${entityNameVar}Mapper.insert(record);
	}
<#if dtoFlag=='N'>
	@Override
	public ${entityName} selectByPrimaryKey(Integer id) {
		return ${entityNameVar}Mapper.selectById(id);
	}
<#else>
	@Override
	public ${entityName}DTO selectByPrimaryKey(Integer id) {
		${entityName} record = ${entityNameVar}Mapper.selectById(id);
		${entityName}DTO recordDto = new ${entityName}DTO();
		BeanUtils.copyProperties(record,recordDto);
		return recordDto;
	}
</#if>
	@Transactional(readOnly = false,rollbackFor = Exception.class,propagation= Propagation.REQUIRED)
	@Override
	public void deletePrimaryKey(Integer id) {
		${entityNameVar}Mapper.deleteById(id);
	}

<#if singleSqlFlag ==false>
	@Override
	public IPage<${entityName}> selectEntityList(Page<${entityName}> page,${entityName} record) {
	QueryWrapper<${entityName}> wrapper = new QueryWrapper<>();
	<#list searchKeys as item>
		<#if item.cronName=='='>
			wrapper.eq(!StringToolUtils.isEmptyObj(record.get${item.beanName}()),"${item.keyName}",record.get${item.beanName}());
		</#if>
		<#if item.cronName=='>'>
			wrapper.gt(!StringToolUtils.isEmptyObj(record.get${item.beanName}()),"${item.keyName}",record.get${item.beanName}());
		</#if>
		<#if item.cronName=='<'>
			wrapper.lt(!StringToolUtils.isEmptyObj(record.get${item.beanName}()),"${item.keyName}",record.get${item.beanName}());
		</#if>
		<#if item.cronName=='>='>
			wrapper.ge(!StringToolUtils.isEmptyObj(record.get${item.beanName}()),"${item.keyName}",record.get${item.beanName}());
		</#if>
		<#if item.cronName=='<='>
			wrapper.le(!StringToolUtils.isEmptyObj(record.get${item.beanName}()),"${item.keyName}",record.get${item.beanName}());
		</#if>
		<#if item.cronName=='like'>
			wrapper.like(!StringToolUtils.isEmptyObj(record.get${item.beanName}()),"${item.keyName}",record.get${item.beanName}());
		</#if>
	</#list>
		return ${entityNameVar}Mapper.selectPage(page,wrapper);
	}
<#else>
	@Override
	public IPage<${entityName}DTO> selectEntityList(Page<${entityName}DTO> page,${entityName} record) {
		Map<String,Object> params = new HashMap<>();
		<#list sqlArgs as item>
		<#if item.argsType=='IN'>
		List<String> list = Arrays.asList(record.${item.argsValue}.split(","));
		params.put("${item.argsKey}",list.isEmpty()?null:list);
			<#else>
		params.put("${item.argsKey}",StringToolUtils.isEmptyObj(record.${item.argsValue})?null:record.${item.argsValue});
			</#if>
		</#list>
		return ${entityNameVar}Mapper.selectEntityList(page,params);
	}
</#if>
	@Transactional(readOnly = false,rollbackFor = Exception.class,propagation= Propagation.REQUIRED)
	@Override
	public void updateEntity(${entityName} record) {
		${entityNameVar}Mapper.updateById(record);
	}

}
